<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Classify;
use Illuminate\Http\Request;

class ClassifyController extends CommonController
{
    /**
     * Display a listing of the resource.
     *列表
     */
    public function index()
    {
        //withTrashed 包换软删除
        $data=Classify::orderBy('id','desc')->paginate($this->page);
        return view('admin.classify.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *添加视图
     */
    public function create()
    {
        //
        return view('admin.classify.create');
    }

    /**
     * Store a newly created resource in storage.
     *  添加处理
     */
    public function store(Request $request)
    {
      $post= $this->validate($request,[
          'name'=>'required|unique:classifies,name',
       ]);
       Classify::create($post);
       return redirect(route('admin.classify.index'))->with(['error'=>'添加成功']);
    }

    /**
     * Display the specified resource
     * 根据id显示
     */
    public function show(Classify $classify)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *  修改视图
     */
    public function edit(Classify $classify)
    {
        return view('admin.classify.edit',compact('classify'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Classify  $classify
     */
    public function update(Request $request, Classify $classify)
    {
        $post=$this->validate($request,[
            'name'=>'required|unique:classifies,name,'.$classify->id,
        ]);
        $classify->update($post);
        return redirect(route('admin.classify.index'))->with(['error'=>'修改成功']);
    }

    /**
     * Remove the specified resource from storage.
     *
     */
    public function destroy(Classify $classify)
    {
        $classify->delete();
        return ['status'=>0,'msg'=>'删除成功'];
    }
}
